package cn.zzuli.activemq.acknowledge;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JmsProduce {
    private static final String ACTIVEMQ_URL = "tcp://81.70.205.143:61617";

    private static final String QUEUE_NAME = "queue";

    public static void main(String[] args) throws JMSException {


        //1.创建链接工厂
        ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        //2.通过连接工厂获得获得链接connection,并启动访问
        Connection connection = mqConnectionFactory.createConnection();
        connection.start();


        //3.创建会话session
        //3.1第一个参数 事务，第二个参数 签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //4.创建目的地，此处使用队列
        Queue queue = session.createQueue(QUEUE_NAME);


        //5.创建消息生产者
        MessageProducer producer = session.createProducer(queue);

        //6.使用session生产3条消息 并使用消息生产者发送消息到mq的队列
        for (int i = 1; i <= 100; i++) {
            //7.生产消息
            TextMessage textMessage = session.createTextMessage("msg -->> " + i);

            //8.通过消息生产者，发送给mq
            producer.send(textMessage);
        }
        //9.关闭资源
        producer.close();
        session.close();
        connection.close();

        System.out.println("******消息发送完成");

    }
}
